package com.zys.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.zys.pojo.Rights;
import com.zys.dao.RightDao;
import com.zys.utils.SerializeUtils;
import com.zys.service.RightService;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RightServiceImpl implements RightService {
  @Autowired
  private RightDao rightDao;

  @Autowired
  private JedisPool jedisPool;

  @Override
  public List<Rights> findAll() throws Exception {
    Jedis jedis = jedisPool.getResource();
    byte[] bytes = jedis.hget("rightList".getBytes(), "rightList".getBytes());

    // 判断 redis 是否有该有数据
    if (bytes != null && bytes.length > 0) {
      return (ArrayList<Rights>) SerializeUtils.deserialize(bytes);
    }

    List<Rights> rightsList = rightDao.findAll();

    jedis.hset("rightList".getBytes(), "rightList".getBytes(), SerializeUtils.serialize(rightsList));

    return rightsList;
  }
}
